export default defineEventHandler(async (event) => {
  const query = getQuery(event)
  
  // 获取查询参数
  const page = Number(query.page) || 1
  const pageSize = Number(query.pageSize) || 12
  const category = query.category as string || ''
  const tech = query.tech as string || ''
  const difficulty = query.difficulty as string || ''
  const sortBy = query.sort as string || 'latest'
  
  // 处理价格范围
  let priceMin = 0
  let priceMax = 10000
  if (query.priceRange && Array.isArray(query.priceRange)) {
    priceMin = Number(query.priceRange[0]) || 0
    priceMax = Number(query.priceRange[1]) || 10000
  }
  
  // 完整的产品数据库（模拟）
  const allProducts = [
    {
      id: '1',
      title: 'Vue3 + Vite 后台管理系统',
      description: '基于 Vue3、Vite、TypeScript、Element Plus 的后台管理系统，包含权限管理、数据可视化等功能',
      price: 299,
      originalPrice: 599,
      cover: 'https://picsum.photos/seed/admin-system/600/400',
      category: 'frontend',
      tags: ['Vue3', 'TypeScript', 'Element Plus'],
      tech: ['Vue3', 'Vite', 'TypeScript', 'Element Plus', 'Pinia'],
      difficulty: 'intermediate',
      rating: 4.8,
      reviews: 128,
      downloads: 1580,
      createdAt: '2025-01-01',
      updatedAt: '2025-01-10',
    },
    {
      id: '2',
      title: 'React18 电商系统全栈项目',
      description: '完整的电商系统，包含用户端、管理后台、支付集成、订单管理等功能',
      price: 499,
      originalPrice: 999,
      cover: 'https://picsum.photos/seed/ecommerce/600/400',
      category: 'fullstack',
      tags: ['React', 'Node.js', 'MongoDB'],
      tech: ['React18', 'Next.js', 'Node.js', 'Express', 'MongoDB'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 256,
      downloads: 2340,
      createdAt: '2025-01-05',
      updatedAt: '2025-01-12',
    },
    {
      id: '3',
      title: 'Nuxt3 内容管理系统',
      description: '基于 Nuxt3 的内容管理系统，支持 Markdown、SEO 优化、多语言等功能',
      price: 399,
      originalPrice: 799,
      cover: 'https://picsum.photos/seed/cms/600/400',
      category: 'fullstack',
      tags: ['Nuxt3', 'Vue3', 'TailwindCSS'],
      tech: ['Nuxt3', 'Vue3', 'Nuxt Content', 'TailwindCSS'],
      difficulty: 'intermediate',
      rating: 4.7,
      reviews: 89,
      downloads: 956,
      createdAt: '2025-01-08',
      updatedAt: '2025-01-11',
    },
    {
      id: '4',
      title: 'NestJS + TypeORM 企业级后端',
      description: '企业级 RESTful API 项目，包含认证授权、数据库操作、文件上传等功能',
      price: 599,
      originalPrice: 1299,
      cover: 'https://picsum.photos/seed/backend/600/400',
      category: 'backend',
      tags: ['NestJS', 'TypeScript', 'MySQL'],
      tech: ['NestJS', 'TypeScript', 'TypeORM', 'MySQL', 'Redis'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 178,
      downloads: 1876,
      createdAt: '2025-01-03',
      updatedAt: '2025-01-09',
    },
    {
      id: '5',
      title: 'uniapp 跨平台商城小程序',
      description: 'uniapp 开发的跨平台商城，支持微信小程序、H5、App 多端',
      price: 699,
      originalPrice: 1499,
      cover: 'https://picsum.photos/seed/miniapp/600/400',
      category: 'mobile',
      tags: ['uniapp', 'Vue3', '微信小程序'],
      tech: ['uniapp', 'Vue3', '微信小程序', 'uni-ui'],
      difficulty: 'intermediate',
      rating: 4.6,
      reviews: 145,
      downloads: 1234,
      createdAt: '2025-01-06',
      updatedAt: '2025-01-13',
    },
    {
      id: '6',
      title: 'Three.js 3D 可视化大屏',
      description: '基于 Three.js 的 3D 数据可视化大屏，包含粒子效果、动画等',
      price: 799,
      originalPrice: 1599,
      cover: 'https://picsum.photos/seed/3d-screen/600/400',
      category: 'frontend',
      tags: ['Three.js', 'WebGL', '数据可视化'],
      tech: ['Three.js', 'Vue3', 'ECharts', 'WebGL'],
      difficulty: 'advanced',
      rating: 5.0,
      reviews: 67,
      downloads: 789,
      createdAt: '2025-01-10',
      updatedAt: '2025-01-14',
    },
    {
      id: '7',
      title: 'Flutter 跨平台 APP',
      description: 'Flutter 开发的跨平台移动应用，包含完整的用户系统和社交功能',
      price: 899,
      originalPrice: 1899,
      cover: 'https://picsum.photos/seed/flutter-app/600/400',
      category: 'mobile',
      tags: ['Flutter', 'Dart', 'iOS/Android'],
      tech: ['Flutter', 'Dart', 'Firebase', 'GetX'],
      difficulty: 'advanced',
      rating: 4.8,
      reviews: 112,
      downloads: 998,
      createdAt: '2025-01-07',
      updatedAt: '2025-01-12',
    },
    {
      id: '8',
      title: 'React Native 社交 APP',
      description: 'React Native 开发的社交应用，支持聊天、动态、直播等功能',
      price: 999,
      originalPrice: 2199,
      cover: 'https://picsum.photos/seed/rn-social/600/400',
      category: 'mobile',
      tags: ['React Native', 'TypeScript', '社交'],
      tech: ['React Native', 'TypeScript', 'Redux', 'Socket.io'],
      difficulty: 'advanced',
      rating: 4.7,
      reviews: 95,
      downloads: 876,
      createdAt: '2025-01-04',
      updatedAt: '2025-01-11',
    },
    {
      id: '9',
      title: 'Vue3 仿抖音短视频系统',
      description: '仿抖音的短视频应用，支持视频上传、点赞、评论、关注等功能',
      price: 1299,
      originalPrice: 2599,
      cover: 'https://picsum.photos/seed/douyin/600/400',
      category: 'fullstack',
      tags: ['Vue3', '短视频', '社交'],
      tech: ['Vue3', 'Node.js', 'MongoDB', 'FFmpeg'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 234,
      downloads: 1678,
      createdAt: '2024-12-28',
      updatedAt: '2025-01-08',
    },
    {
      id: '10',
      title: 'SpringBoot 微服务电商平台',
      description: 'SpringBoot + SpringCloud 微服务架构电商平台，分布式部署',
      price: 1999,
      originalPrice: 3999,
      cover: 'https://picsum.photos/seed/springboot/600/400',
      category: 'backend',
      tags: ['SpringBoot', 'Java', '微服务'],
      tech: ['SpringBoot', 'SpringCloud', 'MySQL', 'Redis', 'RabbitMQ'],
      difficulty: 'advanced',
      rating: 4.8,
      reviews: 189,
      downloads: 2345,
      createdAt: '2024-12-25',
      updatedAt: '2025-01-15',
    },
    {
      id: '11',
      title: 'Vue3 仿网易云音乐',
      description: '仿网易云音乐的音乐播放器，支持歌单、搜索、播放等功能',
      price: 199,
      originalPrice: 399,
      cover: 'https://picsum.photos/seed/music/600/400',
      category: 'frontend',
      tags: ['Vue3', '音乐', 'UI'],
      tech: ['Vue3', 'Vite', 'Pinia', 'TailwindCSS'],
      difficulty: 'beginner',
      rating: 4.5,
      reviews: 156,
      downloads: 2890,
      createdAt: '2025-01-02',
      updatedAt: '2025-01-09',
    },
    {
      id: '12',
      title: 'React 在线协作白板',
      description: '实时协作白板应用，支持多人同时绘画、文字、形状等工具',
      price: 599,
      originalPrice: 1199,
      cover: 'https://picsum.photos/seed/whiteboard/600/400',
      category: 'frontend',
      tags: ['React', 'Canvas', 'WebSocket'],
      tech: ['React', 'Canvas', 'Socket.io', 'Fabric.js'],
      difficulty: 'advanced',
      rating: 4.7,
      reviews: 78,
      downloads: 567,
      createdAt: '2024-12-30',
      updatedAt: '2025-01-11',
    },
    {
      id: '13',
      title: 'Python + Django 博客系统',
      description: 'Django 开发的博客系统，支持文章发布、分类、标签、评论等功能',
      price: 299,
      originalPrice: 599,
      cover: 'https://picsum.photos/seed/django-blog/600/400',
      category: 'backend',
      tags: ['Python', 'Django', '博客'],
      tech: ['Python', 'Django', 'PostgreSQL', 'Celery'],
      difficulty: 'intermediate',
      rating: 4.6,
      reviews: 123,
      downloads: 1456,
      createdAt: '2024-12-26',
      updatedAt: '2025-01-07',
    },
    {
      id: '14',
      title: 'Next.js 企业官网模板',
      description: 'Next.js 开发的企业官网模板，包含首页、产品、案例、联系等页面',
      price: 399,
      originalPrice: 799,
      cover: 'https://picsum.photos/seed/nextjs-site/600/400',
      category: 'frontend',
      tags: ['Next.js', 'React', 'SEO'],
      tech: ['Next.js', 'React', 'TailwindCSS', 'Framer Motion'],
      difficulty: 'intermediate',
      rating: 4.8,
      reviews: 201,
      downloads: 3456,
      createdAt: '2024-12-29',
      updatedAt: '2025-01-10',
    },
    {
      id: '15',
      title: 'Go + Gin 即时通讯系统',
      description: 'Go 语言开发的即时通讯系统，支持单聊、群聊、文件传输等功能',
      price: 899,
      originalPrice: 1799,
      cover: 'https://picsum.photos/seed/go-im/600/400',
      category: 'backend',
      tags: ['Go', 'WebSocket', '即时通讯'],
      tech: ['Go', 'Gin', 'WebSocket', 'MySQL', 'Redis'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 167,
      downloads: 1234,
      createdAt: '2024-12-27',
      updatedAt: '2025-01-13',
    },
    {
      id: '16',
      title: 'Vue3 数据可视化大屏',
      description: '智慧城市数据可视化大屏，支持多种图表展示和数据实时更新',
      price: 699,
      originalPrice: 1399,
      cover: 'https://picsum.photos/seed/data-screen/600/400',
      category: 'frontend',
      tags: ['Vue3', 'ECharts', '可视化'],
      tech: ['Vue3', 'ECharts', 'DataV', 'WebSocket'],
      difficulty: 'intermediate',
      rating: 4.7,
      reviews: 145,
      downloads: 987,
      createdAt: '2025-01-04',
      updatedAt: '2025-01-12',
    },
    {
      id: '17',
      title: 'React 在线教育平台',
      description: '在线教育平台，包含课程管理、视频播放、作业提交、在线考试等功能',
      price: 1599,
      originalPrice: 3199,
      cover: 'https://picsum.photos/seed/education/600/400',
      category: 'fullstack',
      tags: ['React', 'Node.js', '教育'],
      tech: ['React', 'Node.js', 'MongoDB', 'FFmpeg', 'Socket.io'],
      difficulty: 'advanced',
      rating: 4.8,
      reviews: 298,
      downloads: 2456,
      createdAt: '2024-12-24',
      updatedAt: '2025-01-14',
    },
    {
      id: '18',
      title: 'Nuxt3 博客系统',
      description: 'Nuxt3 开发的个人博客，支持 Markdown、代码高亮、评论等功能',
      price: 199,
      originalPrice: 399,
      cover: 'https://picsum.photos/seed/nuxt-blog/600/400',
      category: 'frontend',
      tags: ['Nuxt3', 'Vue3', '博客'],
      tech: ['Nuxt3', 'Vue3', 'Nuxt Content', 'TailwindCSS'],
      difficulty: 'beginner',
      rating: 4.6,
      reviews: 189,
      downloads: 3678,
      createdAt: '2025-01-01',
      updatedAt: '2025-01-08',
    },
    {
      id: '19',
      title: 'Laravel 在线商城系统',
      description: 'Laravel 开发的电商系统，包含商品管理、订单管理、支付集成等功能',
      price: 799,
      originalPrice: 1599,
      cover: 'https://picsum.photos/seed/laravel-shop/600/400',
      category: 'backend',
      tags: ['PHP', 'Laravel', '电商'],
      tech: ['Laravel', 'PHP', 'MySQL', 'Redis', 'Vue3'],
      difficulty: 'intermediate',
      rating: 4.7,
      reviews: 234,
      downloads: 2789,
      createdAt: '2024-12-23',
      updatedAt: '2025-01-09',
    },
    {
      id: '20',
      title: 'Vue3 移动端UI组件库',
      description: '基于 Vue3 的移动端 UI 组件库，包含 50+ 常用组件',
      price: 299,
      originalPrice: 599,
      cover: 'https://picsum.photos/seed/mobile-ui/600/400',
      category: 'frontend',
      tags: ['Vue3', 'UI组件', '移动端'],
      tech: ['Vue3', 'TypeScript', 'Vite', 'Less'],
      difficulty: 'intermediate',
      rating: 4.9,
      reviews: 456,
      downloads: 5678,
      createdAt: '2024-12-20',
      updatedAt: '2025-01-15',
    },
    {
      id: '21',
      title: 'React Admin Pro 后台模板',
      description: 'React 专业版后台管理模板，包含丰富的页面和组件',
      price: 499,
      originalPrice: 999,
      cover: 'https://picsum.photos/seed/react-admin/600/400',
      category: 'frontend',
      tags: ['React', 'Ant Design', '后台'],
      tech: ['React', 'Ant Design', 'UmiJS', 'TypeScript'],
      difficulty: 'intermediate',
      rating: 4.8,
      reviews: 345,
      downloads: 4567,
      createdAt: '2024-12-22',
      updatedAt: '2025-01-13',
    },
    {
      id: '22',
      title: 'Node.js 爬虫系统',
      description: '基于 Node.js 的网页爬虫系统，支持多线程、代理、反反爬等功能',
      price: 599,
      originalPrice: 1199,
      cover: 'https://picsum.photos/seed/crawler/600/400',
      category: 'backend',
      tags: ['Node.js', '爬虫', '数据采集'],
      tech: ['Node.js', 'Puppeteer', 'Cheerio', 'MongoDB'],
      difficulty: 'advanced',
      rating: 4.7,
      reviews: 123,
      downloads: 1567,
      createdAt: '2024-12-21',
      updatedAt: '2025-01-12',
    },
    {
      id: '23',
      title: 'Vue3 低代码平台',
      description: 'Vue3 低代码平台，拖拽生成页面，支持组件配置和代码导出',
      price: 1999,
      originalPrice: 3999,
      cover: 'https://picsum.photos/seed/lowcode/600/400',
      category: 'fullstack',
      tags: ['Vue3', '低代码', '可视化'],
      tech: ['Vue3', 'TypeScript', 'Monaco Editor', 'Node.js'],
      difficulty: 'advanced',
      rating: 4.9,
      reviews: 167,
      downloads: 1234,
      createdAt: '2024-12-18',
      updatedAt: '2025-01-16',
    },
    {
      id: '24',
      title: 'React 图片编辑器',
      description: '在线图片编辑器，支持裁剪、滤镜、贴纸、文字等功能',
      price: 799,
      originalPrice: 1599,
      cover: 'https://picsum.photos/seed/image-editor/600/400',
      category: 'frontend',
      tags: ['React', 'Canvas', '图片处理'],
      tech: ['React', 'Canvas', 'Fabric.js', 'TypeScript'],
      difficulty: 'advanced',
      rating: 4.8,
      reviews: 198,
      downloads: 2345,
      createdAt: '2024-12-19',
      updatedAt: '2025-01-14',
    },
  ]
  
  // 1. 筛选
  let filteredProducts = allProducts.filter(product => {
    // 分类筛选
    if (category && product.category !== category) {
      return false
    }
    
    // 技术栈筛选
    if (tech && !product.tech.includes(tech)) {
      return false
    }
    
    // 难度筛选
    if (difficulty && product.difficulty !== difficulty) {
      return false
    }
    
    // 价格范围筛选
    if (product.price < priceMin || product.price > priceMax) {
      return false
    }
    
    return true
  })
  
  // 2. 排序
  switch (sortBy) {
    case 'latest':
      filteredProducts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
      break
    case 'popular':
      filteredProducts.sort((a, b) => b.downloads - a.downloads)
      break
    case 'price-asc':
      filteredProducts.sort((a, b) => a.price - b.price)
      break
    case 'price-desc':
      filteredProducts.sort((a, b) => b.price - a.price)
      break
    case 'rating':
      filteredProducts.sort((a, b) => b.rating - a.rating)
      break
  }
  
  // 3. 分页
  const total = filteredProducts.length
  const totalPages = Math.ceil(total / pageSize)
  const start = (page - 1) * pageSize
  const end = start + pageSize
  const paginatedProducts = filteredProducts.slice(start, end)
  
  // 4. 为每个产品添加 images 字段（基于 cover 生成多张图片）
  const items = paginatedProducts.map(product => ({
    ...product,
    difficulty: product.difficulty as 'beginner' | 'intermediate' | 'advanced',
    images: [
      product.cover,
      product.cover.replace('/600/400', '/600/400?random=1'),
      product.cover.replace('/600/400', '/600/400?random=2'),
      product.cover.replace('/600/400', '/600/400?random=3'),
    ]
  }))
  
  return {
    code: 200,
    message: 'success',
    data: {
      items,
      pagination: {
        page,
        pageSize,
        total,
        totalPages
      }
    }
  }
})

